# -*- coding: utf-8 -*-

# __date:       2021/7/2
# __author:     Yang Chao
# __function:

class Solution:

    # 解法一： 用元组进行扫描
    def intToRoman1(self, num: int) -> str:
        temp = ((1000, "M"), (900, "CM"),
                (500, "D"), (400, "CD"),
                (100, "C"), (90, "XC"),
                (50, "L"), (40, "XL"),
                (10, "X"), (9, "IX"),
                (5, "V"), (4, "IV"), (1, "I"))

        res = ""
        for i, j in temp:
           ans = num // i
           if ans:
               res += j * ans
               num -= i * ans
        return res

    # 解法二： 从小数位往高位遍历
    def intToRoman2(self, num: int) -> str:

        def getRes(num: int, count: int):
            tens = ["I", "X", "C", "M"]
            fives = ["V", "L", "D"]
            ans = ""
            if num <= 3:
                ans += tens[count] * num
            elif num == 4:
                ans += tens[count] + fives[count]
            elif num == 5:
                ans += fives[count]
            elif num == 9:
                ans += tens[count] + tens[count + 1]
            else:
                ans += fives[count] + tens[count] * (num - 5)
            return ans

        res = ""
        count = 0
        while num:
            cur = num % 10
            res = getRes(cur, count) + res
            count += 1
            num //= 10
        return res


if __name__ == '__main__':
    s = Solution()
    s.intToRoman2(3)